//
//  PrivacyViewController.m
//  安好
//
//  Created by 冯凯 on 14-11-26.
//  Copyright (c) 2014年 lianchuangbrother. All rights reserved.
//

#import "PrivacyViewController.h"
#import "setSecondCell.h"

@interface PrivacyViewController ()<NearBySwitchDelegate>

@end

@implementation PrivacyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //加载导航条
    [self makeNavBar];
    //加载数据
    [self loadData];
    //加载tableView
    [self createTableView];
    
    [[NSNotificationCenter defaultCenter] addObserverForName:@"Privacy" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification 	*note){
        [self.systemTableView reloadData];
    }];
}
/**
 *  存一个状态，为了方便确定是否上传隐私设置
 *
 *  @param animated 无
 */
- (void)viewWillAppear:(BOOL)animated
{
    NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
    NSString * uid = [[UserManager shareInstance]getUserInfo].uid;
    NSString * privacy = [NSString stringWithFormat:@"Privaty%@",uid];
    [user setObject:@"privaty" forKey:privacy];
}
-(void)loadData
{
    self.setArray = @[@"对附近的人可见"];
    self.ideaArray = @[@"对附近的安友可见",@"仅对附近的医生可见"];
}
#pragma mark - 配置导航条
-(void)makeNavBar
{
    self.navigationItem.title = @"隐私设置";
    // 返回按钮图片
    UIBarButtonItem *backBtnImg = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"公用_返回.png"] style:UIBarButtonItemStylePlain target:self action:@selector(backMoreVC)];
    backBtnImg.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = backBtnImg;
}
#pragma mark - 创建tableView设置代理
-(void)createTableView
{
    self.systemTableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0 , kScreenWidth, kScreenHeight - iOS7TopMargin) style:UITableViewStyleGrouped];
    self.systemTableView.delegate = self;
    self.systemTableView.dataSource = self;
    //隐藏横向和竖向的滑条
    self.systemTableView.showsHorizontalScrollIndicator = NO;
    self.systemTableView.showsVerticalScrollIndicator = NO;
    self.systemTableView.backgroundColor = [ClassMethodsViewController colorWithHexString:BACK_BACKGROUNDCOLOR_16];
    [self.view addSubview:self.systemTableView];
}
#pragma mark 返回
- (void)backMoreVC
{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark tableViewDelegate
/**
 *  设置 为 2 段
 */
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
    NSString * uid = [[UserManager shareInstance]getUserInfo].uid;
    NSString * near = [NSString stringWithFormat:@"AllNear%@",uid];
    NSString * nearAll = [user objectForKey:near];
    if ([nearAll isEqualToString:@"3"]|| [nearAll length] == 0) {
        return 2;
    }else{
        return 1;
    }
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
    NSString * uid = [[UserManager shareInstance]getUserInfo].uid;
    NSString * near = [NSString stringWithFormat:@"AllNear%@",uid];
    NSString * nearAll = [user objectForKey:near];
    if ([nearAll isEqualToString:@"3"]|| [nearAll length] == 0) {
        if (section == 0)
        {
            return self.setArray.count;
        }else{
            return self.ideaArray.count;
        }
    }else{
        return self.setArray.count;
    }
}
//设置段头高
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
   return 10;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (kScreenWidth == 414){
        return 56;
    }else{
        return 44;
    }
}
#pragma mark ----2中cell的加载
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    setSecondCell *cell = [tableView dequeueReusableCellWithIdentifier:@"second"];
    if (cell == nil) {
        if (kScreenWidth == 414) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"setSecondCell" owner:self options:nil] lastObject];
            
        }else{
            cell = [[[NSBundle mainBundle] loadNibNamed:@"setSecondCell" owner:self options:nil] firstObject];

        }
    }
    [cell.isSwitch addTarget:self action:@selector(isSwitch:) forControlEvents:UIControlEventTouchUpInside];
    NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
    NSString * uid = [[UserManager shareInstance]getUserInfo].uid;
//    NSString * near = [NSString stringWithFormat:@"AllNear%@",uid];
    NSString * near = [NSString stringWithFormat:@"AllNear%@",uid];

    NSString * nearAll = [user objectForKey:near];
    if (indexPath.section == 0) {
        if ([nearAll isEqualToString:@"3"]|| [nearAll length] == 0) {
            cell.isSwitch.on = YES;
        }else{
            cell.isSwitch.on = NO;
        }
        cell.isSwitch.tag = 10+indexPath.row;
        cell.titleLable.text = self.setArray[indexPath.row];
    }else{
        if (indexPath.row == 0) {
            NSString * near = [NSString stringWithFormat:@"AnNear%@",uid];
            NSString * anNearString = [user objectForKey:near];
            if ([anNearString length] == 0 || [anNearString isEqualToString:@"1"]) {
                cell.isSwitch.on = YES;
            }else{
                cell.isSwitch.on = NO;
            }
        }else{
            NSString * near = [NSString stringWithFormat:@"DoctionNear%@",uid];
            NSString * anNearString = [user objectForKey:near];
            if ([anNearString length] == 0 || [anNearString isEqualToString:@"2"]) {
                cell.isSwitch.on = YES;
            }else{
                cell.isSwitch.on = NO;
            }
        }
        cell.isSwitch.tag = 20+indexPath.row;
        cell.titleLable.text = self.ideaArray[indexPath.row];
    }
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
  UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 10)];
  return view;
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
/**
 *  开关  判断开启的是哪个状态
 *
 *  @param isSwitch 无
 */
- (void)isSwitch:(UISwitch *)isSwitch
{
    if (isSwitch.tag == 10){
        if(isSwitch.on){
            NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
            NSString * uid = [[UserManager shareInstance]getUserInfo].uid;
            NSString * near = [NSString stringWithFormat:@"AllNear%@",uid];
            [user setObject:@"3" forKey:near];
            [self performSelector:@selector(delayMethod) withObject:nil afterDelay:0.3f];
        }else{
            NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
            NSString * uid = [[UserManager shareInstance]getUserInfo].uid;
            NSString * near = [NSString stringWithFormat:@"AllNear%@",uid];
            [user setObject:@"4" forKey:near];
            [self performSelector:@selector(delayMethod) withObject:nil afterDelay:0.3f];
        }
    }else if (isSwitch.tag == 20){
        if (isSwitch.on) {
            NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
            NSString * uid = [[UserManager shareInstance]getUserInfo].uid;
            NSString * near = [NSString stringWithFormat:@"AnNear%@",uid];
            [user setObject:@"1" forKey:near];
        }else{
            NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
            NSString * uid = [[UserManager shareInstance]getUserInfo].uid;
            NSString * near = [NSString stringWithFormat:@"AnNear%@",uid];
            [user setObject:@"0" forKey:near];
        }

    }else{
        if (isSwitch.on) {
            NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
            NSString * uid = [[UserManager shareInstance]getUserInfo].uid;
            NSString * near = [NSString stringWithFormat:@"DoctionNear%@",uid];
            [user setObject:@"2" forKey:near];
        }else{
            NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
            NSString * uid = [[UserManager shareInstance]getUserInfo].uid;
            NSString * near = [NSString stringWithFormat:@"DoctionNear%@",uid];
            [user setObject:@"0" forKey:near];
        }
    }
}
/**
 *  开关控制  刷新tableView
 */
- (void)delayMethod
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"Privacy" object:nil];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)changeSwitch:(UISwitch *)sender
{
    UserModel * userModel = [[UserManager shareInstance]getUserInfo];
    NSUserDefaults *userdefault=[NSUserDefaults standardUserDefaults];
    if (sender.tag==10) {
        if (!sender.on)
        {
            [userdefault setObject:@"4" forKey:[NSString stringWithFormat:@"%@nearBy",userModel.uid]];
        }
        
    }else if (sender.tag==20)
    {
        if (sender.on) {
            UISwitch *swith=(UISwitch *)[self.view viewWithTag:21];
            if (swith.on) {
                [userdefault setObject:@"3" forKey:[NSString stringWithFormat:@"%@nearBy",userModel.uid]];
            }else
            {
                [userdefault setObject:@"1" forKey:[NSString stringWithFormat:@"%@nearBy",userModel.uid]];
            }
        }
        else
        {
            UISwitch *swith=(UISwitch *)[self.view viewWithTag:21];
            if (swith.on) {
                [userdefault setObject:@"2" forKey:[NSString stringWithFormat:@"%@nearBy",userModel.uid]];
            }else
            {
                [userdefault setObject:@"4" forKey:[NSString stringWithFormat:@"%@nearBy",userModel.uid]];
            }
        }

    }
    else if (sender.tag==21)
    {
        if (sender.on) {
            UISwitch *swith=(UISwitch *)[self.view viewWithTag:20];
            if (swith.on) {
                [userdefault setObject:@"3" forKey:[NSString stringWithFormat:@"%@nearBy",userModel.uid]];
            }else
            {
                [userdefault setObject:@"2" forKey:[NSString stringWithFormat:@"%@nearBy",userModel.uid]];
            }

        }
        else
        {
            UISwitch *swith=(UISwitch *)[self.view viewWithTag:20];
            if (swith.on) {
                [userdefault setObject:@"1" forKey:[NSString stringWithFormat:@"%@nearBy",userModel.uid]];
            }else
            {
                [userdefault setObject:@"4" forKey:[NSString stringWithFormat:@"%@nearBy",userModel.uid]];
            }
        }
        
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
